$(function() {
	//获取输入框
	var company = $("#company");
	var num = $("#num");
	var tel = $("#tel");
	var explain = $("#explain");
	var fileElem = $("#fileElem"); //上传按钮
	var fileList = $("#fileList"); //盛放容器
	var ids = []; //存放图片的id

	var mainid = localStorage.getItem("mainid");
	var fenid = localStorage.getItem("fenid");

	//图片选择事件
	fileElem.change(function() {
		var files = this.files;
		var sibling = $(".img-list").children().length;
		if(sibling + files.length >= 7) { //     限制图片上传张数   不能超过6张
			pop("图片不能超过六张");
			fileElem.prop("disabled", true);
			$(".tuku").hide();
		} else { //上传图片
			postImgData(files);
		}
	});

	// 上传图片时提交表单
	function postImgData(files) {
		var formData = new FormData($("#uploadForm")[0]);
		// formData.delete("advice");
		$.ajax({
			url: "http://a.91jfk.com/Product/Ordershop/ImgUpload",
			type: 'post',
			cache: false,
			data: formData,
			processData: false,
			contentType: false,
			success: function(xhr) {
				// console.log(xhr,typeof(xhr));
				var data = JSON.parse(xhr);
				// var data=xhr;

				if(data.code == 1) {
					var imgs = data.message;
					$.each(imgs, function(i) {
						var div = $("<div class='img fl'></div>");
						var span = $("<span class='del'>—</span>");
						var input = $("<input type='hidden' value=" + imgs[i].id + ">");
						var img = $('<img src= "' +imgs[i].src + '">');
						div.append(span).append(input).append(img);
						fileList.append(div);
						ids.push(imgs[i].id);
						//删除图片
						span.bind("click", function() {
							var that = $(this);
							var delId = $(this).next().val();
							$.ajax({
								url: "http://a.91jfk.com/Product/Ordershop/delpic",
								type: "post",
								data: {
									id: delId
								},
								datatype: 'json',
								success: function(xhr) {
									// var data=xhr;
									var data = JSON.parse(xhr);
									if(data.code == 1) {
										that.parent().remove();
										ids.slice(1, 1);
									}
								}
							})
						})
					})
				} else {
					pop(data.message);
				}
			},
			error: function(xhr) {
				//var data=JSON.parse(xhr);
				var data = xhr;
				pop(data.message);
			}
		})

	}
	//提交之后进行判断
	$(".fill_logistics_footer").click(function(e) {
		//阻止冒泡
		e.preventDefault();
		var companyText = company.val();
		var numText = num.val();
		var telText = tel.val();
		var explain = $("#explain").val();
		var id = ids.join(",");
		//判断输入框是否为空
		if(companyText == "") {
			pop("物流公司不能为空");
		} else if(numText == "") {
			pop("物流单号不能为空");
		} else if(!isTel(telText)) {
			pop("请输入正确的手机号");
		} else {
			//序列化表单值的作用是将表单中的值拼装成字符串形式的key-value键值对提交给后台服务器程序解析，来获取用户的输入值
			console.log(mainid, fenid, id, companyText, numText);
			$.ajax({
				url: 'http://a.91jfk.com/Product/Ordershop/ForReturnExprressInfo',
				type: 'post',
				data: {
					expressname: companyText,
					expressnum: numText,
					pic_id: id,
					orderid: mainid,
					branchid: fenid,
					refund_descript:explain
				},
				success: function(xhr) {
					var data = JSON.parse(xhr);
					if(data.code == 1) {
						console.log('123456');
						pop("提交成功！");
						setTimeout(function() {
							window.location.href = "waitReceiptOrder.html";
						}, 2000);
					} else {
						pop(data.message);
						pop(data.message);
					}
				},
				error: function(res) {
					pop(res);
				}
			});
		}
	});

	//电话验证，包括座机和手机
	function isTel(tel) {
		var rule = /(\(\d{3,4}\)|\d{3,4}-|\s)?\d{7,14}/;
		return rule.test(tel);
	}
	//提示弹框
	function pop(a) {
		$(".errAlert").text(a).addClass("AlertShow");
		setTimeout(function() {
			$(".errAlert").removeClass("AlertShow");
		}, 1000);
	}

});